<?php

include dirname(__FILE__). '/AController.php';

class OwnerController extends AController
{
	
	public $defaultAction = "list";
	
	public function actionList() {
		$block_title = '加盟账号列表';
		$arr = array(
			'pagination' => array('pageSize' => 20),
			'criteria' => array('order' => 'owner_id DESC')
		);
		if (key_exists('inuse', $_GET)) {
			$arr['criteria']['condition'] = 'inuse='.intval($_GET['inuse']);
			$str = array('0' => ' - 待审用', '1' => ' - 已通过');
			$block_title .= $str[$_GET['inuse']];
		}
		
		$dataProvider = new CActiveDataProvider('owner', $arr);
		$this->render('list', array('block_title' => $block_title, 'dataProvider' => $dataProvider));
	}
	
	/**
	 * 更新加盟账号的可用状态
	 */
	public function actionUpdate() {
		if (key_exists('inuse', $_GET) && !empty($_POST['owners'])) {
			foreach ($_POST['owners'] as $k => $owner_id) {
				Owner::model()->updateByPk($owner_id, array('inuse' => intval($_GET['inuse'])));
			}
		}
		$this->redirect(array('list'));
	}
	
	/**
	 * 修改加盟账号的详细 账号信息
	 * @param string $owner_id
	 */
	public function actionEdit($owner_id) {
		$this->layout = '/layouts/popMain';
		$model = Owner::model()->find('loginid=:owner_id', array(':owner_id' => $owner_id));
		if(isset($_POST['ajax']) && $_POST['ajax']==='owner-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		$this->render('view', array('block_title' => '账号信息', 'model' => $model));
	}
	
	public function actionDelete() {
		if (!empty($_POST['owners'])) {
			foreach ($_POST['owners'] as $k => $owner_id) {
				Owner::model()->deleteByPk($owner_id);
			}
		}
		$this->redirect(array('list'));
	}
}